package com.teacher.pc.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teacher.common.CommonResult;
import com.teacher.pojo.dto.stuclass.StuClassAddDto;
import com.teacher.pojo.dto.stuclass.StuClassGetDto;
import com.teacher.pojo.dto.stuclass.StuClassUpdateDto;
import com.teacher.pojo.entity.StuClass;
import com.teacher.pojo.vo.stuclass.StuClassGetVo;
import com.teacher.service.IStuClassService;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zfm
 * @since 2021-07-17
 */
@RestController
@RequestMapping("/pc/stu-class")
public class StuClassController {

    @Autowired
    private IStuClassService iStuClassService;

    /**
     * 添加班级
     *
     * @param classAddDto
     * @return
     */
    @ApiOperation("添加班级 stuClass_add")
    @PostMapping
    @PreAuthorize("hasAuthority('stuClass_add')")
    public CommonResult<String> add(@Validated @RequestBody StuClassAddDto classAddDto) {
        StuClass stuClass = new StuClass();
        BeanUtils.copyProperties(classAddDto, stuClass);
        if (iStuClassService.save(stuClass)) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    /**
     * 更新班级信息
     *
     * @param dto
     * @return
     */
    @ApiOperation("更新班级信息 stuClass_update")
    @PutMapping
    @PreAuthorize("hasAuthority('stuClass_update')")
    public CommonResult<String> update(@Validated @RequestBody StuClassUpdateDto dto) {
        StuClass stuClass = new StuClass();
        BeanUtils.copyProperties(dto, stuClass);
        if (iStuClassService.updateById(stuClass)) {
            return CommonResult.success("更新成功");
        }
        return CommonResult.failed("更新失败");
    }

    /**
     * 删除班级
     *
     * @param ids
     * @return
     */
    @ApiOperation("删除班级 stuClass_delete")
    @DeleteMapping
    @PreAuthorize("hasAuthority('stuClass_delete')")
    public CommonResult<String> del(@RequestBody List<Long> ids) {
        if (iStuClassService.removeByIds(ids)) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }

    /**
     * 查询班级
     *
     * @param dto
     * @return
     */
    @GetMapping
    @ApiOperation("查询班级 stuClass_get")
    @PreAuthorize("hasAuthority('stuClass_get')")
    public CommonResult<IPage<StuClassGetVo>> get(StuClassGetDto dto) {
        return iStuClassService.getPage(dto);
    }

    /**
     * 远程搜索班级-带权限
     *
     * @param name
     * @return
     */
    @GetMapping("/byName")
    @ApiOperation("远程搜索班级-带权限")
    public CommonResult<List<StuClass>> getByName(String name) {
        return iStuClassService.getByName(name);
    }

    /**
     * 远程搜索班级-不带权限
     *
     * @param name
     * @return
     */
    @GetMapping("/byNameNotPermission")
    @ApiOperation("远程搜索班级-不带权限")
    public CommonResult<List<StuClass>> getByNameNotPermission(String name) {
        return CommonResult.success(iStuClassService.list(new QueryWrapper<StuClass>().lambda()
                .like(StrUtil.isNotEmpty(name), StuClass::getClassName, name)
                .eq(StuClass::getStatus, 10)));
    }

    /**
     * 远程搜索班级-带毕业班
     *
     * @param name
     * @return
     */
    @GetMapping("/byNameNotPermissionAndAll")
    @ApiOperation("远程搜索班级-带毕业班")
    public CommonResult<List<StuClass>> getByNameNotPermissionAndAll(String name) {
        return CommonResult.success(iStuClassService.list(new QueryWrapper<StuClass>().lambda()
                .like(StrUtil.isNotEmpty(name), StuClass::getClassName, name)));
    }

    /**
     * 获取可查看年级
     *
     * @return
     */
    @GetMapping("/year")
    @ApiOperation("获取可查看年级")
    public CommonResult<List<String>> getYearList() {
        return iStuClassService.getYearList();
    }

}

